package smlab.effects
{
	
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	
	/**
	 * 透明渐变倒影
	 * @author simon
	 * Dustin Andrew
	 */
	public class Shade_SM extends Sprite {
		
		private var _disTarget:DisplayObject;
		private var _numStartFade:Number = .3;
		private var _numMidLoc:Number = .5;
		private var _numEndFade:Number = 0;
		private var _numSkewX:Number = 0;
		private var _distance:Number;
		private var _numScale:Number = 1;		
		private var _bmpReflect:Bitmap;
		
		/**
		 * @param target		目标
		 * @param startFade	
		 * @param midLoc
		 * @param endFade		渐变终点
		 * @param skewX			X轴倾斜度
		 * @param $scale		缩放
		 * @param distance		倒影与原物的距离
		 * 倒影需要另外 addChild 到与原物同一 parent 下（不用再设置xy）
		 * 
		 */
		public function Shade_SM(target:DisplayObject, startFade:Number, midLoc:Number, endFade:Number, skewX:Number, $scale:Number,distance:Number=1)
		{
			super()
			_disTarget = target;
			_numStartFade = startFade;
			_numMidLoc = midLoc;
			_numEndFade = endFade;
			_numSkewX = skewX;
			_numScale = $scale;
			_distance=distance;
			
			_bmpReflect = new Bitmap(new BitmapData(1, 1, true, 0));
			this.addChild(_bmpReflect);
			createReflection();
		}
		
		private function createReflection(event:Event = null):void 
		{
			var bmpDraw:BitmapData = new BitmapData(_disTarget.width, _disTarget.height, true, 0);
			var matSkew:Matrix = new Matrix(1, 0, _numSkewX, -1 * _numScale, 0, _disTarget.height);
			var recDraw:Rectangle = new Rectangle(0, 0, _disTarget.width, _disTarget.height * (2 - _numScale));
			var potSkew:Point = matSkew.transformPoint(new Point(0, _disTarget.height));
			matSkew.tx = potSkew.x * -1;
			matSkew.ty = (potSkew.y - _disTarget.height) * -1;
			bmpDraw.draw(_disTarget, matSkew, null, null, recDraw, true);
			
			var shpDraw:Shape = new Shape();
			var matGrad:Matrix = new Matrix();
			var arrAlpha:Array = new Array(_numStartFade, (_numStartFade - _numEndFade) / 2, _numEndFade);
			var arrMatrix:Array = new Array(0, 0xFF * _numMidLoc, 0xFF);
			matGrad.createGradientBox(_disTarget.width, _disTarget.height, 0.5 * Math.PI);
			shpDraw.graphics.beginGradientFill(GradientType.LINEAR, new Array(0,0,0), arrAlpha, arrMatrix, matGrad)
			shpDraw.graphics.drawRect(0, 0, _disTarget.width, _disTarget.height);
			shpDraw.graphics.endFill();
			bmpDraw.draw(shpDraw, null, null, BlendMode.ALPHA);
			
			_bmpReflect.bitmapData.dispose();
			_bmpReflect.bitmapData = bmpDraw;
			
			_bmpReflect.filters = _disTarget.filters;
			
			this.x = _disTarget.x;
			this.y = (_disTarget.y + _disTarget.height) + _distance;          
		}
	}
}

/*
使用实例

package {

import com.flashdev.bitmap.*;	
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.filters.*;

public class Shade_SMTest extends Sprite {

public function Shade_SMTest() {
// Setup stage
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;

// Load image
var loadImg:Loader = new Loader();
loadImg.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded);
loadImg.load(new URLRequest("image.jpg"));
}

// Called when image is loaded
private function onImgLoaded(event:Event):void {

var sprImg:Sprite = new Sprite();

// Attach image
var disImg:DisplayObject = event.target.content;
var filGlow:GlowFilter = new GlowFilter(0x000000, .3, 8, 8, 1, 3, false, false);
disImg.filters = [filGlow];
sprImg.addChild(disImg);

// Add reflection
var sprReflect:Shade_SM = new Shade_SM(disImg, .4, .2, 0, 0, 1);
sprImg.addChild(sprReflect);			

// Center
sprImg.x = (this.stage.stageWidth - sprImg.width) / 2;
sprImg.y = (this.stage.stageHeight - sprImg.height) / 2;

this.addChild(sprImg);
}
}
}
*/